package com.xzw.test.springcloud.command;

import com.netflix.hystrix.HystrixCommand;
import com.xzw.test.springcloud.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class MyHystrixCommand extends HystrixCommand {

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    //@Autowired
    private RestTemplate restTemplate;

    private Long id;

    public MyHystrixCommand(Setter setter,RestTemplate restTemplate,Long id) {
        super(setter);
        this.restTemplate = restTemplate;
        this.id = id;
    }

    @Override
    protected Object run() throws Exception {
        log.info("===MyHystrixCommand 执行===");
        //String forObject = restTemplate.getForObject(PAYMENT_URL + "/payment/get/id={1}", String.class, id);
        String forObject = restTemplate.getForObject(PAYMENT_URL + "/payment/get/id={1}", String.class, 1L);
        log.info("结果返回 forObject:{}",forObject);
        return forObject;
    }

    @Override
    protected Object getFallback() {
        return "error";
    }
}
